package com.party.admin.biz.merchantAnalyze;

import com.party.common.paging.Page;
import com.party.common.utils.BigDecimalUtils;
import com.party.core.model.member.*;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.member.*;
import com.party.core.service.order.IOrderFormService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 合作商总统计业务接口
 * Created by wei.li.
 *
 * @date 2017/12/12 0012
 * @time 10:01
 */
@Service
public class MemberMerchantCountBizService {

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private IMemberActService memberActService;

    @Autowired
    private IMemberMerchantCountService memberMerchantCountService;

    @Autowired
    private IPartnerMemberService partnerMemberService;

    @Autowired
    private IOrderFormService orderFormService;

    @Autowired
    private IMemberMerchantDetailService memberMerchantDetailService;

    @Autowired
    private IProjectService projectService;

    /**
     * 统计总表数据初始化
     */
    public void init(){
        List<MemberGroup> list = memberGroupService.getAllPartnerNoOpenStatus();
        for (MemberGroup m : list){
            String mId = m.getId();

            MemberMerchantCount memberMerchantCount = memberMerchantCountService.getSet(mId);
            //报名
            Integer applyNum = memberActService.count(mId, false);

            //会员
            Integer memberNum = partnerMemberService.count(mId, false);
            Integer realNameNum = partnerMemberService.count(mId, true);

            //订单
            Integer orderNum = orderFormService.count(mId, false);

            //金额
            Double payment = orderFormService.paymentSum(mId, false);

            // 成功数
            Integer successNum = projectService.countBySuccessDate(null, mId);

            //浏览量
            MemberMerchantDetailQuery query = new MemberMerchantDetailQuery();
            query.setMemberId(mId);
            MemberMerchantDetail memberMerchantDetail = memberMerchantDetailService.sum(query);

            memberMerchantCount.setApplyNum(applyNum);
            memberMerchantCount.setMemberNum(memberNum);
            memberMerchantCount.setRealMemberNum(realNameNum);
            memberMerchantCount.setOrderNum(orderNum);
            memberMerchantCount.setMoney(payment);
            memberMerchantCount.setViewNum(memberMerchantDetail.getViewNum());
            memberMerchantCount.setSuccessNum(successNum);
            memberMerchantCountService.update(memberMerchantCount);
        }
    }

    /**
     * 校正数据统计总数
     *
     * @param oldMemberId 旧的机构id
     * @param newMemberId 新的机构id
     */
    public void reviseMerchantCount(String oldMemberId, String newMemberId) {
        MemberMerchantCount oldMerchant = memberMerchantCountService.findByMemberId(oldMemberId);
        if (oldMerchant != null) {
            MemberMerchantCount newMerchant = memberMerchantCountService.findByMemberId(newMemberId);
            if (newMerchant != null) {
                Integer orderNum = newMerchant.getOrderNum() + oldMerchant.getOrderNum();
                newMerchant.setOrderNum(orderNum);

                Double money = BigDecimalUtils.add(newMerchant.getMoney(), oldMerchant.getMoney());
                newMerchant.setMoney(money);

                Long viewNum = newMerchant.getViewNum() + oldMerchant.getViewNum();
                newMerchant.setViewNum(viewNum);

                memberMerchantCountService.update(newMerchant);
            }
            oldMerchant.setViewNum(0L);
            oldMerchant.setMoney(0d);
            oldMerchant.setOrderNum(0);
            memberMerchantCountService.update(oldMerchant);
        }
    }

    /**
     * 初始化成功数
     */
    public void initSuccessNum() {
        int pageIndex = 1;
        List<MemberMerchantCount> list;
        MemberMerchantCount param = new MemberMerchantCount();

        Page page = new Page();
        page.setPage(pageIndex);
        page.setLimit(1000);

        while (true) {
            list = memberMerchantCountService.listPage(param, page);
            if (CollectionUtils.isEmpty(list)) {
                break;
            }
            list.forEach(count -> {
                Integer successNum = projectService.countBySuccessDate(null, count.getMemberId());
                if (null == successNum || 0 == successNum) {
                    return;
                }
                count.setSuccessNum(successNum);
                memberMerchantCountService.update(count);
            });
            page.setPage(++pageIndex);
        }
    }
}
